package com.lin.services.impl;

import com.lin.dao.WaresDao;
import com.lin.pojo.Wares;
import com.lin.pojo.Unit;
import com.lin.services.WaresService;
import com.lin.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:WaresServiceImpl
 * Package:com.lin.services.impl
 * Author: hellcat
 * Date: 2019/12/22
 * ♣♣♣
 */
@Service
public class
WaresServiceImpl implements WaresService {
    @Autowired
    private WaresDao waresDao;

    @Override
    public List<Wares> getWarsList(String id, String category1, String category2, String status) {
        try {
            return waresDao.getWarsList(id, category1, category2, status, ShiroUtil.getUser().getStoreId());
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Map<String, Object> deleteWares(String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            waresDao.deleteWares(id, ShiroUtil.getUser().getStoreId());
            map.put("code", true);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "删除失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> findUnit() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Unit> list = waresDao.getUnit(ShiroUtil.getUser().getStoreId());
            map.put("code", true);
            map.put("unit", list);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "数据拉取失败");

        }
        return map;
    }

    @Override
    public Map<String, Object> insertUnit(String unit) {
        Map<String, Object> map = new HashMap<>();
        try {
            Unit unit2 = waresDao.getUnitByUnit(unit, ShiroUtil.getUser().getStoreId());
            if (unit2 != null) {
                map.put("code", false);
                map.put("msg", "单位已存在");
                return map;
            }
            Unit unit1 = new Unit();
            unit1.setUnit(unit);
            unit1.setStoreId(ShiroUtil.getUser().getStoreId());
            waresDao.insertUnitByUnit(unit1);
            map.put("code", true);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "单位创建失败");
        }
        return map;
    }


    @Override
    public Map<String, Object> checkMessage() {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> overdue = waresDao.checkWaresOverdue(ShiroUtil.getUser().getStoreId());
        List<Map<String, Object>> insufficient = waresDao.checkWaresInventoryTooLittle(ShiroUtil.getUser().getStoreId());
        List<Map<String, Object>> inventoryTooMany = waresDao.checkWaresInventoryTooMany(ShiroUtil.getUser().getStoreId());
        map.put("overdue", overdue);
        map.put("insufficient", insufficient);
        map.put("inventoryTooMany", inventoryTooMany);
        return map;
    }

    @Override
    public Wares getCategoryById(String id) {
        return waresDao.getCategoryById(Integer.parseInt(id), ShiroUtil.getUser().getStoreId());

    }

    @Override
    public Map<String, Object> updateWares(Wares wares) {
        Map<String, Object> map = new HashMap<>();
        try {
            wares.setStoreId(ShiroUtil.getUser().getStoreId());
            waresDao.updateWare(wares);
            map.put("code", true);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "无效修改");
        }
        return map;
    }

    @Override
    public Map<String, Object> insertWares(Wares goods) {
        Map<String, Object> map = new HashMap<>();
        try {
            Wares wares = waresDao.findWaresById(goods.getId(), ShiroUtil.getUser().getStoreId());
            if (wares != null) {
                map.put("code", false);
                map.put("msg", "商品条码重复");
                map.put("waresId", wares.getId());
                return map;
            }
            goods.setStoreId(ShiroUtil.getUser().getStoreId());
            waresDao.insertWares(goods);
            map.put("code", true);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "添加失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> changeStatus(String ids) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer storeId = ShiroUtil.getUser().getStoreId();
            String[] split = ids.split(",");
            for (String id : split) {
                waresDao.changeStatus(id, storeId);
            }
            map.put("code", true);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "下架失败");
        }

        return map;
    }

}
